package com.haimi.shiguang.business.user.center;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.haimi.shiguang.R;
import com.haimi.shiguang.api.ApiServeice;
import com.haimi.shiguang.base.BaseFragment;
import com.haimi.shiguang.base.biz.BaseResponse;
import com.haimi.shiguang.base.biz.ResultStatus;
import com.haimi.shiguang.base.dialog.ProgressManager;
import com.haimi.shiguang.bean.response.CenterResponse;
import com.haimi.shiguang.utils.AmountUtils;
import com.haimi.shiguang.utils.ToastUtil;
import com.haimi.shiguang.utils.roundimage.CircleImageView;

import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

/**
 * @author changsunhaipeng
 * @date 2018/6/23
 */

public class CenterFragment extends BaseFragment {
    public static final String TAG = CenterFragment.class.getSimpleName();
    private ImageView mSetting;
    private CircleImageView mAvatar;
    private TextView mNickName;
    private TextView mPoint;
    private TextView mLeftAmount;
    private TextView mLever;
    private TextView mOrder;
    private TextView mFoot;
    private TextView mJoin;
    private TextView mCollection;
    private TextView mFadeUp;
    private TextView mEditInfo;
    private TextView mCoupon;

    public static CenterFragment newInstance() {
        Bundle args = new Bundle();
        CenterFragment fragment = new CenterFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_center, container, false);
    }

    @Override
    public void initView() {
        mSetting = rootView.findViewById(R.id.iv_setting);
        mAvatar = rootView.findViewById(R.id.iv_avatar);
        mNickName = rootView.findViewById(R.id.tv_nick_name);
        mPoint = rootView.findViewById(R.id.tv_point);
        mLeftAmount = rootView.findViewById(R.id.tv_left_amount);
        mLever = rootView.findViewById(R.id.tv_lever);
        mOrder = rootView.findViewById(R.id.tv_my_order);
        mFoot = rootView.findViewById(R.id.tv_my_foot);
        mJoin = rootView.findViewById(R.id.tv_to_join);
        mCollection = rootView.findViewById(R.id.tv_collection_list);
        mFadeUp = rootView.findViewById(R.id.tv_fade_up);
        mEditInfo = rootView.findViewById(R.id.tv_edit_user_info);
        mCoupon = rootView.findViewById(R.id.tv_my_coupon);

        mSetting.setOnClickListener(this);
        mOrder.setOnClickListener(this);
        mFoot.setOnClickListener(this);
        mJoin.setOnClickListener(this);
        mCollection.setOnClickListener(this);
        mFadeUp.setOnClickListener(this);
        mEditInfo.setOnClickListener(this);
        mCoupon.setOnClickListener(this);
    }

    @Override
    public void initListener() {

    }

    @Override
    public void initData() {
        ProgressManager.showProgress(this);
        Disposable disposable = ApiServeice.getInstance()
                .center()
                .subscribe(new Consumer<BaseResponse<CenterResponse>>() {
                    @Override
                    public void accept(BaseResponse<CenterResponse> centerResponse) throws Exception {
                        ProgressManager.closeProgress(CenterFragment.this);
                        if (ResultStatus.success.equals(centerResponse.status)) {
                            refreshView(centerResponse.data);
                        } else {
                            ToastUtil.showToast(centerResponse.message);
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        ProgressManager.closeProgress(CenterFragment.this);
                        ToastUtil.showToast("加载失败");
                    }
                });
        compositeDisposable.add(disposable);
    }

    private void refreshView(CenterResponse data) {
        if (data == null) {
            return;
        }
        Glide.with(getActivity()).load(data.customer.headImg).into(mAvatar);
        if (TextUtils.isEmpty(data.customer.nickName)) {
            mNickName.setText(data.customer.mobile);
        } else {
            mNickName.setText(data.customer.nickName);
        }
        mPoint.setText(data.account.point + "");
        mLeftAmount.setText(AmountUtils.changeF2Y(data.account.AccountMoney));
        mLever.setText(data.account.accountLevelName);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_setting:
                ToastUtil.showToast("666");
                break;
            case R.id.tv_my_order:
                ToastUtil.showToast("666");
                break;
            case R.id.tv_my_foot:
                ToastUtil.showToast("666");
                break;
            case R.id.tv_to_join:
                ToastUtil.showToast("666");
                break;
            case R.id.tv_collection_list:
                ToastUtil.showToast("666");
                break;
            case R.id.tv_fade_up:
                ToastUtil.showToast("666");
                break;
            case R.id.tv_my_coupon:
                ToastUtil.showToast("666");
                break;
            case R.id.tv_edit_user_info:
                ToastUtil.showToast("666");
                break;
            default:
                break;
        }
    }
}
